#include "CS2103T_CE1.h"

using namespace std;

/*---------------------------------------------------------------
1. Assuming that the text is stored and can be reused by the programme.
That is, if the file provided exists, read and parse. Else, create
the file.

2. Assuming that the numbering is not stored into txtfile (since it's
not part of user input)

3. Assuming that when there are not enough parameters (applicable to
add and delete), prints warning and abort the command
---------------------------------------------------------------*/

void file_to_vector(fstream& file, string filepath, vector<string>& items) {
	string line;
	file.open(filepath, fstream::in);
	while (getline(file, line)) {
		items.push_back(line);
	}
	file.close();
}

void vector_to_file(fstream& file, string filepath, vector<string>& items) {
	file.open(filepath, fstream::out | fstream::trunc);
	if (file.good()) {
		for (int i = 0; i < items.size(); i++) {
			file << items[i] << '\n';
		}
		file.close();
	}

}

std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

void command_add(vector<string>& items, string filepath, vector<string> args = vector<string>()) {
	string txt = "";
	for (unsigned int i = 1; i < args.size(); i++) {
		txt += args[i];
	}
	cout << "added to " << filepath << ": \"" << txt << "\"\n";
	items.push_back(txt);
}

void command_display(vector<string>& items, string filepath, vector<string> args = vector<string>()) {
	for (int i = 1; i <= items.size(); i++) {
		cout << i << ". " << items[i - 1] << '\n';
	}
}

void command_delete(vector<string>& items, string filepath, vector<string> args = vector<string>()) {
	int idx = stoi(args[1]);
	cout << "deleted from " << filepath << ": \"" << items[idx - 1] << "\"\n";
	items.erase(items.begin() + idx - 1);
}

void command_clear(vector<string>& items, string filepath, vector<string> args = vector<string>()) {
	items = vector<string>();
	cout << "all content deleted from " << filepath << '\n';
}

int main(int argc, char* argv []) {
	// If no file arg, exit
	if (argc < 2) {
		cout << "Number of arguments: " << argc << "\nYou didn't provide a file path!\n" << "Terminating...\n";
		return 1;
	}

	vector<string> items;
	string filepath = argv[1];
	fstream file;
	file.open(filepath, fstream::in);
	if (file.is_open()) {
		cout << "File existing, reading...\n";
		file.close();
		file_to_vector(file, filepath, items);
	}
	else {
		cout << "File not exist, creating...\n";
		file.open(filepath, fstream::out | fstream::trunc);
		file.close();
	}

	cout << "Welcome to TextBuddy. " << filepath << " is ready for use.\n";

	bool exit = false;
	while (!exit) {
		cout << "Command: ";
		string command_txt;
		getline(cin, command_txt);

		vector<string> command_arr;
		split(command_txt, ' ', command_arr);

		string command_type = command_arr[0];

		if (command_type == "exit") {
			exit = true;
		}
		else if (command_type == "add") {
			command_add(items, filepath, command_arr);
			vector_to_file(file, filepath, items);
		}
		else if (command_type == "display") {
			command_display(items, filepath, command_arr);
			vector_to_file(file, filepath, items);
		}
		else if (command_type == "delete") {
			command_delete(items, filepath, command_arr);
			vector_to_file(file, filepath, items);
		}
		else if (command_type == "clear") {
			command_clear(items, filepath, command_arr);
			vector_to_file(file, filepath, items);
		}
		else {
			cout << "Command not recognized, please try again.";
		}
	}

	return 0;
}